from tortoise import fields
from app.models.base import TimestampMixin, BaseModel

__all__ = [
    'Admin',
    'SystemRole',
    'SystemMenu',
    'SystemRoleMenu',
    'SystemDept',
    'SystemPost',
]

"""
系统模块，用户鉴权，RBAC权限管理
"""


class Admin(BaseModel, TimestampMixin):
    """管理员实体"""

    id = fields.IntField(pk=True, description='主键')
    role_ids = fields.CharField(max_length=128, null=False, default='', description='用户角色IDs')
    is_super = fields.BooleanField(default=False, description="超级管理员 True:超级管理员 False:普通管理员")
    username = fields.CharField(max_length=32, null=False, default='', description='用户账号')
    password = fields.CharField(max_length=128, null=False, default='', description='用户密码')
    mobile = fields.CharField(max_length=32, null=False, default='', description='用户电话')
    real_name = fields.CharField(max_length=32, null=False, default='', description='头像')
    nickname = fields.CharField(max_length=32, null=False, default='', description='用户昵称')
    avatar = fields.CharField(max_length=200, null=False, default='', description='头像')
    gender = fields.SmallIntField(null=False, default=0, description='用户性别: [0=保密, 1=男, 2=女]')
    status = fields.SmallIntField(null=False, default=1, description='状态: [0=停用, 1=正常]')
    is_deleted = fields.BooleanField(default=False, description='是否删除: [False=正常, True=删除]')

    class Meta:
        table_description = "管理员表"
        table = "system_admin"


class SystemRole(BaseModel, TimestampMixin):
    name = fields.CharField(null=False, default='', max_length=100, description="角色名称")
    remark = fields.CharField(null=True, default='', max_length=255, description='角色描述')
    sort = fields.IntField(null=False, default=0, description='排序')
    status = fields.SmallIntField(null=False, default=1, description='状态: [0=停用, 1=正常]')
    is_deleted = fields.BooleanField(default=False, description='是否删除: [False=正常, True=删除]')

    class Meta:
        table_description = "系统角色表"
        table = "system_role"


class SystemMenu(BaseModel, TimestampMixin):
    pid = fields.BigIntField(default=0, description='上级菜单')
    menu_name = fields.CharField(null=False, default='', max_length=100, description="菜单名称")
    menu_type = fields.CharField(null=False, default='', max_length=100, description="权限类型: M=目录，C=菜单，A=按钮")
    menu_icon = fields.CharField(null=False, default='', max_length=100, description="菜单图标")
    sort = fields.IntField(null=False, default=0, max_length=5, description="菜单排序")
    scopes = fields.CharField(null=True, default='', max_length=255, description='权限标识')
    paths = fields.CharField(null=True, default='', max_length=255, description='路由地址')
    component = fields.CharField(null=True, default='', max_length=255, description='前端组件')
    params = fields.CharField(null=True, default='', max_length=255, description='路由参数')
    is_cache = fields.SmallIntField(null=False, default=0, description='是否缓存: 0=否, 1=是')
    is_show = fields.SmallIntField(null=False, default=0, description='是否显示: 0=否, 1=是')
    status = fields.SmallIntField(null=False, default=1, description='状态: [0=停用, 1=正常]')

    class Meta:
        table_description = "系统角色表"
        table = "system_menu"


class SystemRoleMenu(BaseModel):
    role_id = fields.BigIntField(default=0, description='角色ID')
    menu_id = fields.BigIntField(default=0, description='菜单ID')

    class Meta:
        table_description = "系统角色菜单表"
        table = "system_role_menu"


class SystemDept(BaseModel, TimestampMixin):
    pid = fields.BigIntField(default=0, description='上级部门')
    name = fields.CharField(null=False, default='', max_length=30, description="部门名称")
    leader = fields.CharField(null=True, default='', max_length=30, description="部门领导")
    mobile = fields.CharField(null=True, default='', max_length=11, description='领导联系电话')
    remark = fields.CharField(null=True, default='', max_length=255, description='部门描述')
    sort = fields.IntField(null=False, default=0, description='排序')
    status = fields.SmallIntField(null=False, default=1, description='状态: [0=停用, 1=正常]')
    is_deleted = fields.BooleanField(default=False, description='是否删除: [False=正常, True=删除]')

    class Meta:
        table_description = "系统部门管理表"
        table = "system_dept"


class SystemPost(BaseModel, TimestampMixin):
    name = fields.CharField(null=False, default='', max_length=30, description="岗位名称")
    code = fields.CharField(null=False, default='', max_length=30, description="岗位编码")
    remark = fields.CharField(null=True, default='', max_length=255, description='岗位备注')
    sort = fields.IntField(null=False, default=0, description='排序')
    status = fields.SmallIntField(null=False, default=1, description='状态: [0=停用, 1=正常]')
    is_deleted = fields.BooleanField(default=False, description='是否删除: [False=正常, True=删除]')

    class Meta:
        table_description = "系统菜单管理表"
        table = "system_post"


# class SystemConfig(BaseModel, TimestampMixin):
#     """系统配置实体"""
#     # id = fields.BigIntField(pk=True, index=True, description='主键')
#     type = fields.CharField(max_length=32, null=True, default='', description='类型')
#     name = fields.CharField(max_length=60, null=False, default='', description='键')
#     value = fields.TextField(null=False, default='', description='值')
#
#     class Meta:
#         table_description = "系统配置表"
#         table = "system_config"

#
# class AccessLog(BaseModel, TimestampMixin):
#     user_id = fields.IntField(description="用户ID")
#     target_url = fields.CharField(null=True, description="访问的url", max_length=255)
#     user_agent = fields.CharField(null=True, description="访问UA", max_length=255)
#     request_params = fields.JSONField(null=True, description="请求参数get|post")
#     ip = fields.CharField(null=True, max_length=32, description="访问IP")
#     note = fields.CharField(null=True, max_length=255, description="备注")
#
#     class Meta:
#         table_description = "用户操作记录表"
#         table = "access_log"
